;
; NOTER 
; A note-taking tool 
; Automatically saves whatever you type into it ...
; 
; Copyright (C) 2007 Sandesh Singh (http://sandesh247.googlepages.com/)
; 
; This program is free software; you can redistribute it and/or
; modify it under the terms of the GNU General Public License as
; published by the Free Software Foundation; either version 2 of the
; License, or (at your option) any later version.

; This program is distributed in the hope that it will be useful, but
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; General Public License for more details.

; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
; USA



build := 1
dir := "noteRdata"
file := "note.txt"
noteRIniFilepath := "noteR.ini"
website := "http://sandesh247.googlepages.com/autohotkeyscripts"
noteRDataFile = %dir%/%file%

; Create the file and directories if they dont exist.
IfNotExist, %dir%
    FileCreateDir, %dir%
IfNotExist %noteRDataFile%
    FileAppend, , %noteRDataFile%

; Let's roll!
GoSub ReadNoteRSettings
GoSub CreateNoterGui
GoSub ReadNote

;The hotkey grabber. Brings up the window if it isn't there; or else hides it.
noteR:
SetTitleMatchMode 3
ifWinExist noteR
    IfWinActive noteR
        Gui, Hide
    else
        WinActivate noteR
else
    Gui, Show, , noteR
return


GuiClose:
GuiEscape:
    Gui, Hide
return

noteRExit:
    ; Save size and position  data
    GoSub GetAndSetNoteRDisplaySettings
    Gui, Destroy
    ExitApp
return


; Save the note from the GUI into the file
SaveNote:
    GuiControlGet, MainEdit
    IfExist %noteRDataFile%
    {
        FileDelete %noteRDataFile%
        if ErrorLevel
        {
            MsgBox The attempt to overwrite "%noteRDataFile%" failed.
            return
        }
    }
    FileAppend, %MainEdit%, %noteRDataFile%  ; Save the contents to the file.
return


; Read the note into the GUI
ReadNote:
FileRead, fileText, %noteRDataFile% 
    if ErrorLevel
    {
        MsgBox Could not open "%SelectedFileName%".
        return
    }
    GuiControl, , MainEdit, %fileText%
return



; Save a copy of the note
SaveNoteAs:
    Gui +OwnDialogs  ; Force the user to dismiss the FileSelectFile dialog before returning to the main window.
    FileSelectFile, SelectedFileName, S16,, Save File, Text Documents (*.txt)
    if SelectedFileName =  ; No file selected.
        return
    IfExist %SelectedFileName%
    {
        FileDelete %SelectedFileName%
        if ErrorLevel
        {
            MsgBox The attempt to overwrite "%noteRDataFile%" failed.
            return
        }
    }
    FileAppend, %MainEdit%, %SelectedFileName%  ; Save the contents to the file.return
return

CreateNoterGui:
    menu, tray, NoStandard
    menu, tray, add, About ..., About
    menu, tray, add, Open Website, OpenNoteRWebsite
    menu, tray, add, Preferences, ShowNoteRSettings
    menu, tray, add, Reload, ReloadNoteR
    menu, tray, add, Exit, noteRExit

    ; Make the GUI
    Menu, FileMenu, Add, Save &As, SaveNoteAs
    Menu, FileMenu, Add, &Preferences, ShowNoteRSettings
    Menu, FileMenu, Add, E&xit, noteRExit
    Menu, HelpMenu, Add, Open &Website, OpenNoteRWebsite
    Menu, HelpMenu, Add, &About, About

    Menu, MyMenuBar, Add, &File, :FileMenu
    Menu, MyMenuBar, Add, &Help, :HelpMenu

    ;Gui, Margin, 2, 2
    Gui, Menu, MyMenuBar
    Gui, Color, , %backColor%
    Gui, font, c%foreColor% 
    Gui, Margin, 0, 0
    Gui, Add, Edit, vMainEdit gSaveNote WantTab R2
    Gui, +LastFound +Resize
    Gui, Show, , noteR
    
    if setTransparent < 255
        WinSet, Transparent, %setTransparent%
        
    WinMove, , , leftPos, topPos, width, height
    
    Gui, Hide
return


ShowNoteRSettings:
Gui, 2:Destroy
GoSub GetAndSetNoteRDisplaySettings
HotKey,%shortcutKey%, Off

Gui, 2:Add, Text, +Right, Shortcut (hide/unhide)
Gui, 2:Add, Text, +Right, Horizontal Position
Gui, 2:Add, Text, +Right, Vertical Position
Gui, 2:Add, Text, +Right, Background color
Gui, 2:Add, Text, +Right, Foreground color
Gui, 2:Add, Text, +Right, Width
Gui, 2:Add, Text, +Right, Height
Gui, 2:Add, Text, , Opacity Level

Gui, 2:Add, Hotkey, ym vshortcutKey, %shortcutKey%
Gui, 2:Add, Edit, +Number vleftPos wp, %leftPos%
Gui, 2:Add, Edit, +Number vtopPos wp, %topPos%
Gui, 2:Add, Edit, vbackColor wp, %backColor%
Gui, 2:Add, Edit, vforeColor wp, %foreColor%
Gui, 2:Add, Edit, +Number vwidth wp, %width%
Gui, 2:Add, Edit, +Number vheight wp, %height%
Gui, 2:Add, Slider, vsetTransparent yp+30 Range0-255, %setTransparent%

Gui, 2:Add, Button, xm w50 yp+30 gAcceptNoteRSettings, Ok 
Gui, 2:Add, Button, xp+70 wp gDiscardNoteRSettings, Cancel
Gui, 2:Show, , noteR Settings
Return


AcceptNoteRSettings:
    Gui, 2:Submit
    GoSub WriteNoteRSettings
    Reload
Return

DiscardNoteRSettings:
    Gui, 2:Destroy
Return

SetDefaultNoteRSettings:
    shortcutKey := "^!q"

    leftPos = 20
    topPos = 20
    backColor = 3A6EA5
    foreColor = FFFFFF
    width = 640
    height := 480
    setTransparent := 255
return



ReadNoteRSettings:
    IfNotExist, %noteRIniFilepath%
    {
        GoSub SetDefaultNoteRSettings
        GoSub WriteNoteRSettings
    }
    
    IniRead, iniBuild, %noteRIniFilepath%, Settings, build
    If (iniBuild < build)
    {
        GoSub SetDefaultNoteRSettings
        GoSub WriteNoteRSettings
    }    
    
    IniRead, shortcutKey, %noteRIniFilepath%, Settings, shortcutKey
    IniRead, leftPos, %noteRIniFilepath%, Settings, leftPos
    IniRead, topPos, %noteRIniFilepath%, Settings, topPos
    IniRead, backColor, %noteRIniFilepath%, Settings, backColor
    IniRead, foreColor, %noteRIniFilepath%, Settings, foreColor
    IniRead, width, %noteRIniFilepath%, Settings, width
    IniRead, height, %noteRIniFilepath%, Settings, height
    IniRead, setTransparent, %noteRIniFilepath%, Settings, setTransparent
    
    HotKey,%shortcutKey%, NoteR
return


WriteNoteRSettings:
    IniWrite, %build%, %noteRIniFilepath%, Settings, build
    IniWrite, %shortcutKey%, %noteRIniFilepath%, Settings, shortcutKey
    IniWrite, %leftPos%, %noteRIniFilepath%, Settings, leftPos
    IniWrite, %topPos%, %noteRIniFilepath%, Settings, topPos
    IniWrite, %backColor%, %noteRIniFilepath%, Settings, backColor
    IniWrite, %foreColor%, %noteRIniFilepath%, Settings, foreColor
    IniWrite, %width%, %noteRIniFilepath%, Settings, width
    IniWrite, %height%, %noteRIniFilepath%, Settings, height
    IniWrite, %setTransparent%, %noteRIniFilepath%, Settings, setTransparent
return

; About box
About:
    MsgBox noteR: A note taking tool. By Sandesh Singh (%website%)
return

OpenNoteRWebsite:
    Run %website%
return


GuiSize:
if ErrorLevel = 1  ; The window has been minimized.  No action needed.
    return
; Otherwise, the window has been resized or maximized. Resize the Edit control to match.
newWidth := A_GuiWidth
newHeight := A_GuiHeight
GuiControl, Move, MainEdit, W%newWidth% H%newHeight%
;GoSub GetAndSetNoteRDisplaySettings
return


GetAndSetNoteRDisplaySettings:
    SetTitleMatchMode 3
    WinGetPos leftPos, topPos, width, height, noteR
    GoSub WriteNoteRSettings
return

ReloadNoteR:
    Reload
return
